package com.doubleblacktech.datamanager
{
	import flash.data.SQLResult;
	import flash.data.SQLStatement;
	import flash.events.Event;
	import flash.events.SQLErrorEvent;
	
	import mx.collections.ArrayCollection;
	
	[Event(name="queryCompleted", type="com.doubleblacktech.datamanager.fxaQueryEvent")]
	[Event(name="dataError", type="com.doubleblacktech.datamanager.FxADataError")]
	
	[Bindable]
	public class FxASqlStatement extends SQLStatement
	{
		private var _dataResults:ArrayCollection;
		private var _name:String;
		private var _text:String;
		private var _sqlParams:Object;
		private var _isExecuting:Boolean = false;
		private var _inSync:Boolean = false;
		
		private var _recordCount:int = 0;
		
		private var _isPrepared:Boolean = false;
		private var _preFetch:int = -1;
		private var _isComplete:Boolean = false;
		private var _lastInsertedRowId:Number = 0;
		private var _numRowsAffected:Number = 0;
				
		private var myConnMan:FxAConnectionManager;
		
		public function FxASqlStatement()
		{
			myConnMan = FxAConnectionManager.getInstance();
			
			myConnMan.addEventListener(fxaQueryEvent.QUERY_COMPLETED, queryCompleted_Handler);
		}
		
		private function queryCompleted_Handler(event:Event):void {
		}
		//**************************************************************
		// Public Methods
		//**************************************************************
		public function send():void {
			myConnMan.execute(this, _sqlParams);
		}

		public function setSqlStmtResult(myResult:SQLResult):void {
			this.dataResults = new ArrayCollection(myResult.data);

			this._recordCount = this.dataResults.length;
			this._isComplete = myResult.complete;
			this._lastInsertedRowId = myResult.lastInsertRowID;
			this._numRowsAffected= myResult.rowsAffected;
			
			var myFxaQueryEventVO:fxaQueryEventVO = new fxaQueryEventVO;
			
			myFxaQueryEventVO.data = this.dataResults;
			
			var myFxaQueryEvent:fxaQueryEvent = new fxaQueryEvent('queryCompleted');
			
			myFxaQueryEvent.results = myFxaQueryEventVO;
			myFxaQueryEvent.isEnabled=true;
			
			dispatchEvent(myFxaQueryEvent);
		}
		
		private function dispatchError(event:SQLErrorEvent):void {
			var fxaErrorVO:FxADataErrorVO = new FxADataErrorVO;
			
            fxaErrorVO.errCode = event.error.code;
            fxaErrorVO.errOperation = event.error.operation;
            fxaErrorVO.errMessage = event.error.message;
            fxaErrorVO.errType = 'open';
            
            var fxaError:FxADataError = new FxADataError("dataError");
			fxaError.data = fxaErrorVO;
			
			dispatchEvent(fxaError);
		}
		//**************************************************************
		// Event Handlers
		//**************************************************************
		
		
		//**************************************************************
		// Getters and Setters
		//**************************************************************
		// Read/Write Properties
		override public function get text():String {
			return _text;
		}
		
		override public function set text(value:String):void {
			super.text = value;
			
			_text = value;
		}
		
		public function get name():String {
        	return _name;
    	}
    	
    	public function set name(value:String):void {
        	if (value != null){
        		_name = value;
			} else {
                _name = null;
        	}
		}
		
		public function get dataResults():ArrayCollection {
			return _dataResults;
		}
		
		public function set dataResults(value:ArrayCollection):void {
			_dataResults = value;
		}
		
		public function get sqlParams():Object {
        	return _sqlParams;
    	}
    	
    	public function set sqlParams(value:Object):void {
        	if (value != null){
        		_sqlParams = new Object();
                for (var i:String in value) {
					_sqlParams[i] = value[i];
                }
			} else {
                _sqlParams = null;
        	}
		}
		
		public function get inSync():Boolean {
        	return _inSync;
    	}
    	
    	public function set inSync(value:Boolean):void {
			_inSync = value;
		}
		
		// Read-Only Properties
		public function get isComplete():Boolean {
        	return _isComplete;
    	}
    	
    	public function get recordCount():int {
        	return _recordCount;
    	}
    	
    	public function get lastInsertedRowId():Number {
        	return _lastInsertedRowId;
    	}
    	
    	public function get numRowsAffected():Number {
        	return _numRowsAffected;
    	}
    	
    	public function get isExecuting():Boolean {
   			_isExecuting = this.executing
    			
   			return _isExecuting;
    	}
    	
    	public function get isPrepared():Boolean {
   			_isPrepared = this.prepared;
    			
   			return _isPrepared;
    	}
    	// END: Getters and Setters ***********************************
	}
}